﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Private Members Through Closures</title>
    <script type="text/javascript" src="../js/jquery-1.10.2.js"></script>
    <script type="text/javascript" src="../js/Interface.js"></script>
    <script type="text/javascript" src="../js/Helpers.js"></script>
</head>
<body>
    <div id="content">
    </div>
    <script type="text/javascript">
        var Book = function (newIsbn, newTitle, newAuthor) { // implements Publication
            // Private attributes.
            var isbn, title, author;
            // Private method.
            function checkIsbn(isbn) {
                return true;
            }
            // Privileged methods.
            this.getIsbn = function () {
                return isbn;
            };
            this.setIsbn = function (newIsbn) {
                if (!checkIsbn(newIsbn)) throw new Error('Book: Invalid ISBN.');
                isbn = newIsbn;
            };
            this.getTitle = function () {
                return title;
            };
            this.setTitle = function (newTitle) {
                title = newTitle || 'No title specified';
            };
            this.getAuthor = function () {
                return author;
            };
            this.setAuthor = function (newAuthor) {
                author = newAuthor || 'No author specified';
            };
            // Constructor code.
            this.setIsbn(newIsbn);
            this.setTitle(newTitle);
            this.setAuthor(newAuthor);
        };
        // Public, non-privileged methods.
        Book.prototype = {
            display: function () {
                return String.format("{0} {1} {2}", this.getIsbn(), this.getTitle(), this.getAuthor());
            }
        };

        $(function () {
            // Book(isbn, title, author)
            var theHobbit = new Book('0-395-07122-4', 'The Hobbit', 'J. R. R. Tolkien');
            theHobbit.display(); // Outputs the data by creating and populating an HTML element.

            $("#content").html(theHobbit.display());
        });
    </script>
</body>
</html>
